﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using System.Xml;
using Autodesk.Revit.DB;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.HVAC;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using HYFamilyLibDataManager;

namespace YArchitech.Plumbing
{
	public partial class FormLayPump : System.Windows.Forms.Form
	{
		public FormLayPump(Document doc)
		{
			this.InitializeComponent();
			base.Icon = Resource.YArchitechIcon;
			this.doc = doc;
		}

		private void InitParameterSetting()
		{
			this.dic = this.userSetting.GetLayPumpSetting();
			this.name = this.userSetting.GetValueFromDic("name", "IS单级单吸卧式离心泵", this.dic);
			foreach (HYPumpFactory hypumpFactory in this.listFactory)
			{
				if (hypumpFactory.szTypeInFactory == this.name)
				{
					this.InitPictureBox(hypumpFactory);
					this.listPumpData = HYPumpDataManager.GetTablePumpBasicData(hypumpFactory);
					break;
				}
			}
			this.UpdataCombBoxSPEC(this.listPumpData);
			this.cobSpecification.SelectedIndex = int.Parse(this.userSetting.GetValueFromDic("specIndex", "0", this.dic));
			this.textBoxPumpDistance.Text = this.userSetting.GetValueFromDic("distance", "800", this.dic);
			this.tabControl.SelectedIndex = int.Parse(this.userSetting.GetValueFromDic("page", "0", this.dic));
			try
			{
				this.checkBoxIsDamp.Checked = Convert.ToBoolean(this.userSetting.GetValueFromDic("isDamp", "True", this.dic));
				this.checkBoxLayBase.Checked = Convert.ToBoolean(this.userSetting.GetValueFromDic("isLayBase", "True", this.dic));
				if (!this.checkBoxLayBase.Checked)
				{
					this.tboffset.Text = this.userSetting.GetValueFromDic("offset", "0.3", this.dic);
				}
				else
				{
					this.tboffset.Text = this.userSetting.GetValueFromDic("offset", "0", this.dic);
				}
			}
			catch (Exception)
			{
			}
		}

		private void UserSettingWrite()
		{
			this.tboffset.Text = StringTools.RemoveZero(this.tboffset.Text.Trim());
			this.textBoxPumpDistance.Text = StringTools.RemoveZero(this.textBoxPumpDistance.Text.Trim());
			try
			{
				Dictionary<string, string> dictionary = new Dictionary<string, string>();
				dictionary.Add("name", this.name);
				dictionary.Add("offset", this.tboffset.Text);
				dictionary.Add("distance", this.textBoxPumpDistance.Text);
				dictionary.Add("specIndex", this.cobSpecification.SelectedIndex.ToString());
				dictionary.Add("page", this.tabControl.SelectedIndex.ToString());
				dictionary.Add("isDamp", this.checkBoxIsDamp.Checked.ToString());
				dictionary.Add("isLayBase", this.checkBoxLayBase.Checked.ToString());
				this.userSetting.WriteLayPumpSetting(dictionary);
			}
			catch (Exception)
			{
			}
		}

		private void InitTreeViewPumpTable()
		{
			TreeNode treeNode = new TreeNode("单泵选型方案", 0, 0);
			this.treeViewPumpTable.Nodes.Add(treeNode);
			if (this.listSinglePump.Count > 0)
			{
				foreach (XmlElement xmlElement in this.listSinglePump)
				{
					TreeNode treeNode2 = new TreeNode();
					treeNode2.Text = xmlElement.GetAttribute("SPEC");
					treeNode2.SelectedImageIndex = 1;
					treeNode2.ImageIndex = 1;
					treeNode.Nodes.Add(treeNode2);
					this.AddListPumpData(xmlElement, this.listPumpdataTable);
				}
				this.treeViewPumpTable.SelectedNode = treeNode.Nodes[0];
			}
			TreeNode treeNode3 = new TreeNode("组合泵选型方案", 0, 0);
			this.treeViewPumpTable.Nodes.Add(treeNode3);
			if (this.listGroupPump.Count > 0)
			{
				foreach (XmlElement xmlElement2 in this.listGroupPump)
				{
					TreeNode treeNode4 = new TreeNode();
					treeNode4.Text = xmlElement2.GetAttribute("GroupName");
					if (xmlElement2.GetAttribute("IsPara") == "true")
					{
						treeNode4.ImageIndex = 2;
						treeNode4.SelectedImageIndex = 2;
					}
					else if (xmlElement2.GetAttribute("IsPara") == "false")
					{
						treeNode4.ImageIndex = 3;
						treeNode4.SelectedImageIndex = 3;
					}
					treeNode3.Nodes.Add(treeNode4);
					List<XmlElement> allElements = XMLOperating.GetAllElements(this.xmlDoc, xmlElement2);
					if (allElements.Count > 0)
					{
						foreach (XmlElement pump in allElements)
						{
							this.AddListPumpData(pump, this.listPumpdataTable);
						}
					}
				}
				if (this.listSinglePump.Count < 1)
				{
					this.treeViewPumpTable.SelectedNode = treeNode3.Nodes[0];
				}
			}
			if (this.listPumpdataTable.Count > 1)
			{
				this.listPumpdataTable = this.listPumpdataTable.Distinct<HYPumpData>().ToList<HYPumpData>();
			}
		}

		private void AddListPumpData(XmlElement pump, List<HYPumpData> listPumpdataTable)
		{
			listPumpdataTable.Add(new HYPumpData
			{
				StrSpecification = pump.GetAttribute("SPEC"),
				StrFactory = pump.GetAttribute("Factory"),
				StrTypeInFactory = pump.GetAttribute("TypeInFactory")
			});
		}

		private void InitPictureBox(HYPumpFactory hypumpfactory)
		{
			if (File.Exists(hypumpfactory.szImage2D))
			{
				this.pictureBox.Image = new Bitmap(hypumpfactory.szImage2D);
				return;
			}
			this.pictureBox.Image = null;
		}

		private void UpdataCombBoxSPEC(List<HYPumpData> lists)
		{
			this.cobSpecification.Items.Clear();
			if (lists.Count > 0)
			{
				foreach (HYPumpData hypumpData in lists)
				{
					this.cobSpecification.Items.Add(hypumpData.StrSpecification);
				}
				this.cobSpecification.SelectedIndex = 0;
			}
		}

		public void SetSomeControlState()
		{
			if (this.checkBoxLayBase.Checked)
			{
				this.checkBoxIsDamp.Enabled = true;
				this.labeloffset.Text = "水泵基础相对标高(m):";
				this.isLayBase = true;
				return;
			}
			this.checkBoxIsDamp.Enabled = false;
			this.labeloffset.Text = "水泵相对标高(m):";
			this.isLayBase = false;
		}

		private void FormLayPump_Load(object sender, EventArgs e)
		{
			this.listFactory = HYPumpDataManager.GetAllFactory();
			string text = Path.Combine(Product.UserDataLocation, "HYPumpSelectionResult.XML");
			string sourceFileName = Path.Combine(Product.DataLocation, "User\\HYPumpSelectionResult.XML");
			if (!File.Exists(text))
			{
				File.Copy(sourceFileName, text);
			}
			this.xmlDoc = XMLOperating.LoadXml(text);
			this.listSinglePump = XMLOperating.GetAllElements(this.xmlDoc, "ResultList/SinglePumpList");
			this.listGroupPump = XMLOperating.GetAllElements(this.xmlDoc, "ResultList/GroupPumpList");
			this.InitTreeViewPumpTable();
			this.treeViewPumpTable.ExpandAll();
			this.InitParameterSetting();
			this.SetSomeControlState();
		}

		private void btnOK_Click(object sender, EventArgs e)
		{
			if (this.tboffset.Text != "")
			{
				this.offset = double.Parse(this.tboffset.Text);
				if (this.tabControl.SelectedIndex == 0)
				{
					this.layway = 0;
					this.isDamp = this.checkBoxIsDamp.Checked;
					if (this.pumpdata != null)
					{
						this.UserSettingWrite();
						base.DialogResult = DialogResult.OK;
						return;
					}
				}
				else
				{
					this.layway = 1;
					this.isDamp = this.checkBoxIsDamp.Checked;
					this.listPumpdataTableForLay.Clear();
					for (int i = 0; i < this.checkedListBox.Items.Count; i++)
					{
						if (this.checkedListBox.GetItemChecked(i))
						{
							HYPumpData item = new HYPumpData();
							item = this.listPumpdataTable.FirstOrDefault((HYPumpData q) => q.StrSpecification == this.checkedListBox.SelectedItem.ToString());
							if (HYPumpDataManager.GetSpecPumpData(ref item))
							{
								this.listPumpdataTableForLay.Add(item);
							}
						}
					}
					if (this.listPumpdataTableForLay.Count <= 0)
					{
						YJKMessageBox.Information("没有选中的方案可以布置，请重新选择");
						return;
					}
					if (this.listPumpdataTableForLay.Count <= 1)
					{
						this.UserSettingWrite();
						base.DialogResult = DialogResult.OK;
						return;
					}
					if (this.textBoxPumpDistance.Text.Trim() == "")
					{
						YJKMessageBox.Information("泵组间距不能为空");
						return;
					}
					if (double.Parse(this.textBoxPumpDistance.Text.Trim()) < 1.0)
					{
						YJKMessageBox.Information("泵组间距必须大于1");
						return;
					}
					this.distance = double.Parse(this.textBoxPumpDistance.Text.Trim());
					this.UserSettingWrite();
					base.DialogResult = DialogResult.OK;
					return;
				}
			}
			else
			{
				YJKMessageBox.Information("标高不能为空，请设置标高");
			}
		}

		private void btnCancle_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void pictureBox_Click(object sender, EventArgs e)
		{
			List<ListItem> list = new List<ListItem>();
			if (this.listFactory.Count > 0)
			{
				foreach (HYPumpFactory hypumpFactory in this.listFactory)
				{
					if (File.Exists(hypumpFactory.szImage2D))
					{
						list.Add(new ListItem(hypumpFactory.szTypeInFactory, hypumpFactory.szImage2D));
					}
				}
			}
			FormChooseHydrant formChooseHydrant = new FormChooseHydrant(this.name, list);
			formChooseHydrant.Owner = this;
			formChooseHydrant.Text = "选择水泵";
			if (formChooseHydrant.ShowDialog() == DialogResult.OK && this.name != formChooseHydrant.ImageName && formChooseHydrant.ImageName != "")
			{
				this.name = formChooseHydrant.ImageName;
				foreach (HYPumpFactory hypumpFactory2 in this.listFactory)
				{
					if (hypumpFactory2.szTypeInFactory == this.name)
					{
						this.InitPictureBox(hypumpFactory2);
						this.listPumpData = HYPumpDataManager.GetTablePumpBasicData(hypumpFactory2);
						break;
					}
				}
				this.UpdataCombBoxSPEC(this.listPumpData);
			}
		}

		private void cobSpecification_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.pumpdata = this.listPumpData.FirstOrDefault((HYPumpData q) => q.StrSpecification == this.cobSpecification.SelectedItem.ToString());
			if (HYPumpDataManager.GetSpecPumpData(ref this.pumpdata))
			{
				this.propertyGridDB.SelectedObject = new LayPumpPropertyDB(this.pumpdata.StrSpecification, this.pumpdata.StrFactory, this.pumpdata.StrTypeInFactory, this.pumpdata.DH, this.pumpdata.DQ, this.pumpdata.NRotSpeed, this.pumpdata.DPumpPower, this.pumpdata.DEffection, this.pumpdata.DIn, this.pumpdata.DOut);
			}
		}

		private void checkedListBox_SelectedIndexChanged(object sender, EventArgs e)
		{
			HYPumpData hypumpData = new HYPumpData();
			hypumpData = this.listPumpdataTable.FirstOrDefault((HYPumpData q) => q.StrSpecification == this.checkedListBox.SelectedItem.ToString());
			if (HYPumpDataManager.GetSpecPumpData(ref hypumpData))
			{
				this.propertyGridTable.SelectedObject = new LayPumpPropertyDB(hypumpData.StrSpecification, hypumpData.StrFactory, hypumpData.StrTypeInFactory, hypumpData.DH, hypumpData.DQ, hypumpData.NRotSpeed, hypumpData.DPumpPower, hypumpData.DEffection, hypumpData.DIn, hypumpData.DOut);
			}
		}

		private void treeViewPumpTable_AfterSelect(object sender, TreeViewEventArgs e)
		{
            this.treeViewPumpTable.SelectedNode.BackColor = System.Drawing.Color.DodgerBlue;
            this.treeViewPumpTable.SelectedNode.ForeColor = System.Drawing.Color.White;
			if ((e.Node.Nodes == null || e.Node.Nodes.Count < 1) && e.Node.Parent != null)
			{
				if (e.Node.Parent.Text == "单泵选型方案")
				{
					this.checkedListBox.Items.Clear();
					this.checkedListBox.Items.Add(e.Node.Text);
				}
				else if (e.Node.Parent.Text == "组合泵选型方案")
				{
					string rootNode = "ResultList/GroupPumpList";
					XmlElement xmlNode = XMLOperating.GetXmlNodeByAttribute(this.xmlDoc, rootNode, "GroupName", e.Node.Text) as XmlElement;
					List<XmlElement> allElements = XMLOperating.GetAllElements(this.xmlDoc, xmlNode);
					this.checkedListBox.Items.Clear();
					if (allElements.Count > 0)
					{
						foreach (XmlElement xmlElement in allElements)
						{
							this.checkedListBox.Items.Add(xmlElement.GetAttribute("SPEC"));
						}
					}
				}
				if (this.checkedListBox.Items.Count > 0)
				{
					for (int i = 0; i < this.checkedListBox.Items.Count; i++)
					{
						this.checkedListBox.SetItemCheckState(i, CheckState.Checked);
					}
					this.checkedListBox.SelectedIndex = 0;
				}
			}
		}

		private void treeViewPumpTable_BeforeSelect(object sender, TreeViewCancelEventArgs e)
		{
			if (this.treeViewPumpTable.SelectedNode != null)
			{
                this.treeViewPumpTable.SelectedNode.BackColor = System.Drawing.Color.White;
                this.treeViewPumpTable.SelectedNode.ForeColor = System.Drawing.Color.Black;
			}
		}

		private void checkBoxLayBase_CheckedChanged(object sender, EventArgs e)
		{
			this.SetSomeControlState();
		}

		private void btnMore_Click(object sender, EventArgs e)
		{
			if (FamilyUtil.PathIni.ReadPath(this.doc.Application.VersionNumber) == "")
			{
				YJKMessageBox.Show("请先安装盈建科族立得。");
				return;
			}
			LayFamilyForm layFamilyForm = new LayFamilyForm(new PlumbingDataReader(this.doc).GetFamilyAllInfoTree("水泵"), this.doc);
			if (layFamilyForm.ShowDialog() == DialogResult.OK)
			{
				this.fs = layFamilyForm.fs;
				this.offset = layFamilyForm.OffSet;
				base.DialogResult = DialogResult.OK;
			}
		}

		private List<HYPumpFactory> listFactory = new List<HYPumpFactory>();

		private List<HYPumpData> listPumpData = new List<HYPumpData>();

		public HYPumpData pumpdata = new HYPumpData();

		private string name = "";

		private XmlDocument xmlDoc;

		private List<XmlElement> listSinglePump = new List<XmlElement>();

		private List<XmlElement> listGroupPump = new List<XmlElement>();

		private List<HYPumpData> listPumpdataTable = new List<HYPumpData>();

		public List<HYPumpData> listPumpdataTableForLay = new List<HYPumpData>();

		public int layway;

		public double offset;

		public double distance = 800.0;

		private Dictionary<string, string> dic;

		private HVACUserSetting userSetting = new HVACUserSetting();

		public bool isLayBase = true;

		public bool isDamp = true;

		private Document doc;

		public FamilySymbol fs;
	}
}
